package cn.itcast.bos.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;



@ParentPackage("json-default")
@Namespace("/")
@Action
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {
	
	private Standard standard = new Standard();
	@Autowired
	private StandardService standardService;
	
	@Override
	public Standard getModel() {
		return standard;
	}

	@Action(value="standardAction",results={@Result(name="success",location="./pages/base/standard.html",type="redirect")})
	public String save(){
		System.out.println("1111111");
		standardService.save(standard);
		return SUCCESS;
	}
	
	private int page;
	private int rows;
	
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value="standardPage" ,results={@Result(name="success" , type="json")})
	public String findByPage(){
		//调用业务层 查询数据
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Standard> standards = standardService.findByPage(pageable);
		//返回客户端
		Map<String , Object> map = new HashMap<String , Object>();
		map.put("total",standards.getTotalElements());
		map.put("rows", standards.getContent());
		
		ActionContext.getContext().getValueStack().push(map);
		
		return SUCCESS;
	}
	
	@Action(value="standard_findAll" , results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> standards = standardService.findAll();
		
		ActionContext.getContext().getValueStack().push(standards);
		return SUCCESS;
	}

}
